// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    img_list: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    classList: [],
    teacherList: [],
    video: '',
    ptList: [],
    stList: [],
    nav: [{ name: '直播' }, { name: '录播' }, { name: '面授' }],
    currentId: 0,
    navContent: [],
    id: {},//分类id
    page: 1,
    pagesize: '',
    endloading: false,
    type: 1,
    onOff: ""
  },
  serach() {
    wx.navigateTo({
      url: '/pages/search/index',
    })
  },
  // 分类选择
  sort() {
    wx.navigateTo({
      url: '/pages/sort/index',
    })
  },
  // 直播回播切换
  current(e) {
    this.setData({
      currentId: e.currentTarget.id,
    })
    wx.showLoading({
      title: '加载中',
    })
    if (this.data.currentId == 1) {//录播
      setTimeout(() => {
        wx.hideLoading()
        this.setData({
          page: 1,
          type: 2
        })
        this.getSeo()
      }, 500)
    } else if (this.data.currentId == 2) {//面授
      setTimeout(() => {
        wx.hideLoading()
        this.setData({
          page: 1,
          type: 3
        })
        this.getSeo()
      }, 500)
    } else if (this.data.currentId == 0) {
      setTimeout(() => {
        wx.hideLoading()
        this.setData({
          page: 1,
          type: 1
        })
        this.getSeo()
      }, 500)
    }
  },
  allTeacher() {
    wx.navigateTo({
      url: '/pages/teacher/index',
    })
  },
  allVideo() {
    wx.navigateTo({
      url: '/pages/allVideo/index',
    })
  },
  service() {
    wx.navigateTo({
      url: '/pages/service/index',
    })
  },
  liveDetails(e) {
    let idx = e.currentTarget.id
    let type = this.data.navContent[idx].type
    let pinAllow = this.data.navContent[idx].collage_allow
    if (type == 1 && pinAllow == 0) {//0为直播 type: 1 直播
      wx.navigateTo({
        url: '/pages/liveDetails/index?id=' + e.currentTarget.dataset.id,
      })
    } else if (type == 1 && pinAllow == 1) {
      wx.navigateTo({
        url: '/pages/livePt/index?id=' + e.currentTarget.dataset.id,
      })
    } else if (type == 3 && pinAllow == 0) {
      wx.navigateTo({
        url: '/pages/faceDetails/index?id=' + e.currentTarget.dataset.id,
      })
    } else if (type == 3 && pinAllow == 1) {//2为面授 type: 3 面授
      wx.navigateTo({
        url: '/pages/facePt/index?id=' + e.currentTarget.dataset.id,
      })
    }
  },
  recDetails(e) {
    let idx = e.currentTarget.id
    let pinAllow = this.data.navContent[idx].collage_allow
    if (pinAllow == 0) {
      wx.navigateTo({
        url: '/pages/recDetails/index?id=' + e.currentTarget.dataset.id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/recPt/index?id=' + e.currentTarget.dataset.id,
      })
    }
  },
  allPt(e) {
    wx.navigateTo({
      url: '/pages/allPt/index',
    })
  },
  stClass(e) {
    wx.navigateTo({
      url: '/pages/stClass/index',
    })
  },
  PtDetails(e) {
    let idx = e.currentTarget.id
    let id = e.currentTarget.dataset.id
    let type = this.data.ptList[idx].type
    switch (type) {
      case 1:
        wx.navigateTo({
          url: '/pages/livePt/index?id=' + id,
        })
        break
      case 2:
        wx.navigateTo({
          url: '/pages/recPt/index?id=' + id,
        })
        break
      case 3:
        wx.navigateTo({
          url: '/pages/facePt/index?id=' + id,
        })
        break
    }
  },
  hotClass(e) {
    wx.navigateTo({
      url: '/pages/hotClass/index?id=' + e.currentTarget.dataset.id,
    })
  },
  teacherDetails(e) {
    wx.navigateTo({
      url: '/pages/teacherDetails/index?id=' + e.currentTarget.dataset.id,
    })
  },
  stDetails(e) {
    wx.navigateTo({
      url: '/pages/stDetails/index?id=' + e.currentTarget.dataset.id,
    })
  },
  bannerDetails(e) {
    let idx = e.currentTarget.id
    let id = e.currentTarget.dataset.id
    let type = this.data.img_list[idx].type
    if (type == 1) {
      wx.switchTab({
        url: '/pages/headline/index',
      })
    } else if (type == 2) {
      wx.navigateTo({
        url: '/pages/newsDetails/index?id=' + id,
      })
    } else if (type == 3) {
      wx.switchTab({
        url: '/pages/question/index',
      })
    } else if (type == 4) {
      wx.navigateTo({
        url: '/pages/radio/index?id=' + id,
      })
    } else if (type == 5) {
      wx.navigateTo({
        url: '/pages/addVideo/index?id=' + id,
      })
    } else if (type == 6) {
      wx.navigateTo({
        url: '/pages/hotClass/index?id=' + id,
      })
    } else if (type == 7) {
      wx.navigateTo({
        url: '/pages/teacher/index?id=' + id,
      })
    } else if (type == 8) {
      wx.navigateTo({
        url: '/pages/liveDetails/index?id=' + id,
      })
    } else if (type == 9) {
      wx.navigateTo({
        url: '/pages/livePt/index?id=' + id,
      })
    } else if (type == 10) {
      wx.navigateTo({
        url: '/pages/recDetails/index?id=' + id,
      })
    } else if (type == 11) {
      wx.navigateTo({
        url: '/pages/recPt/index?id=' + id,
      })
    } else if (type == 12) {
      wx.navigateTo({
        url: '/pages/faceDetails/index?id=' + id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/facePt/index?id=' + id,
      })
    }
  },
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    this.getList()
    this.getHotClass()
    this.getTeacher()
    this.getVideo()
  },
  // 轮播图
  getList() {
    let data = {
      method: 'banner.getlist'
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          img_list: res.data,
        })
      }
    })
  },
  // 热门课程
  getHotClass() {
    let data = {
      method: 'cate.gethot',
      cate_id: this.data.id.id
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          classList: res.data,
        })
      }
    })
  },
  // 师资力量
  getTeacher() {
    let data = {
      method: 'teacher.gethot',
      cate_id: this.data.id.id
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          teacherList: res.data,
        })
      }
    })
  },
  // 视频案例
  getVideo() {
    let data = {
      method: 'video.gethot',
      cate_id: this.data.id.id
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          video: res.data,
        })
      }
    })
  },
  // 直播
  getSeo() {
    let data = {
      method: 'course.getlist',
      cate_id: this.data.id.id,
      page: this.data.page,
      type: this.data.type,
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          pagesize: res.data.last_page
        })
        var datas = this.data.navContent
        if (this.data.page == 1) {
          datas = []
        }
        if (res.data.list.lenght == 0) {
          this.setData({
            endloading: true, //上拉不在加载
          })
        } else {
          this.setData({
            page: this.data.page + 1 //更新page 请求下一页数据
          })
          this.setData({
            navContent: datas.concat(res.data.list),
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  // 拼团
  getPt() {
    let data = {
      method: 'course.gethotpin',
      cate_id: this.data.id.id
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          ptList: res.data,
        })
      }
    })
  },
  // 试听
  getSt() {
    let data = {
      method: 'course.gethotshi',
      cate_id: this.data.id.id
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          stList: res.data,
        })
      }
    })
  },
  checked() {
    let data = {
      method: 'banner.onoff',
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          onOff: res.data.onoff
        })
      }
    })
  },
  onShow() {
    this.getList()
    this.getHotClass()
    this.getTeacher()
    this.getVideo()
    this.getPt()
    this.getSt()
    this.getSeo()
    this.checked()
  },
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    wx.setBackgroundTextStyle({
      textStyle: 'dark',// 下拉背景字体、loading 图的样式为dark
    })
    wx.setBackgroundColor({
      backgroundColor: '#666666', // 窗口的背景色为白色
    })
    this.setData({
      page: 1
    })
    setTimeout(() => {
      this.getList()
      this.getHotClass()
      this.getTeacher()
      this.getVideo()
      this.getPt()
      this.getSt()
      this.getSeo()
      wx.stopPullDownRefresh()
      wx.hideNavigationBarLoading()
    }, 1000);
  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    var that = this;
    if (this.data.page <= this.data.pagesize) {
      wx.showLoading({
        title: '加载中'
      })
      setTimeout(() => {
        wx.hideLoading({})
        that.getSeo()  //页面上拉调用这个方法
      }, 1000)
    } else {
      wx.showToast({
        title: '我已经到底了',
        icon: 'none'
      })
    }
  },
})
